package org.lmind.example.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.commons.io.IOUtils;

public final class Readers {
	

	public static String readAll(Reader reader, char[] buf) throws IOException {
		StringBuilder sb = new StringBuilder();
		int r = 0;
		while ((r = reader.read(buf)) != -1) {
			sb.append(buf, 0, r);
		}
		return sb.toString();
	}
	
	public static String readAll(Reader reader) throws IOException {
		return readAll(reader, new char[1024]);
	}
	
	public static String readAll(InputStream input, String charset) throws IOException {
		return readAll(new InputStreamReader(input, charset));
	}
	
	public static String readAll(File file, String charset) throws IOException {
		FileInputStream input = new FileInputStream(file);
		try {
			return readAll(input, charset);
		} finally {
			IOUtils.closeQuietly(input);
		}
	}
}
